package com.xlongwei.accounts.dao;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.noear.solon.annotation.Inject;
import org.noear.solon.test.SolonTest;
import com.xlongwei.accounts.App;
import com.xlongwei.accounts.model.Account;
import com.xlongwei.accounts.model.Ledger;
import com.xlongwei.accounts.model.Member;
import com.xlongwei.accounts.model.User;
import features.SqlTest;

@SolonTest(App.class)
public class UserDaoTest {
    @Inject
    UserDao userDao;
    @Inject
    AccountDao accountDao;
    @Inject
    MemberDao memberDao;
    @Inject
    LedgerDao ledgerDao;

    @Test
    public void user() throws Throwable {
        SqlTest.testAndRollback(() -> {
            UserDao dao = userDao;
            User row = new User();
            row.setMobile(18810761776L);
            row.setPass("123456");
            Integer userid = userDao.create(row);
            row = userDao.retrieve(userid);
            Assertions.assertEquals("123456", row.getPass());
            row.setLevel(2);
            userDao.update(row);
            row = userDao.retrieve(userid);
            Assertions.assertEquals(2, row.getLevel());
            dao.list().stream().forEach(System.out::println);
        });
    }

    @Test
    public void account() {
        SqlTest.testAndRollback(() -> {
            AccountDao dao = accountDao;
            Account row = new Account();
            row.setUserid(1);
            row.setName("现金");
            Integer accountid = accountDao.create(row);
            row = accountDao.retrieve(accountid);
            Assertions.assertEquals("现金", row.getName());
            row.setName("借贷");
            accountDao.update(row);
            row = accountDao.retrieve(accountid);
            Assertions.assertEquals("借贷", row.getName());
            dao.list(1).stream().forEach(System.out::println);
        });
    }

    @Test
    public void member() {
        SqlTest.testAndRollback(() -> {
            MemberDao dao = memberDao;
            Member row = new Member();
            row.setUserid(1);
            row.setName("小孩");
            Integer id = dao.create(row);
            row = dao.retrieve(id);
            Assertions.assertEquals("小孩", row.getName());
            row.setName("老大");
            dao.update(row);
            row = dao.retrieve(id);
            Assertions.assertEquals("老大", row.getName());
            dao.list(1).stream().forEach(System.out::println);
        });
    }

    @Test
    public void ledger() {
        SqlTest.testAndRollback(() -> {
            LedgerDao dao = ledgerDao;
            Ledger row = new Ledger();
            String name = "红包";
            row.setUserid(1);
            row.setType("I");
            row.setName(name);
            Integer id = dao.create(row);
            row = dao.retrieve(id);
            Assertions.assertEquals(name, row.getName());
            row.setName(name = "补贴");
            dao.update(row);
            row = dao.retrieve(id);
            Assertions.assertEquals(name, row.getName());
            dao.list(1).stream().forEach(System.out::println);
        });
    }
}
